//
//  JournalListController.m
//  DataStatistics
//
//  Created by Kang on 16/4/14.
//  Copyright © 2016年 YTYangK. All rights reserved.
//

#import "JournalListController.h"
#import "JournalViewModel.h"              //日志列表模型
#import "EquipmenDetailsViewModel.h"
#import "JournalListCell.h"
//#import "EquipmenDetailsVC.h"            // 详情
//#import "DTKDropdownMenuView.h"  //下拉框工具
#import "UIimageView+WebCache.h"
#import "SystemInfoController.h" //
#define  reuseIdentifier  @"journalList"
#import "GUAAlertView.h"
#import "PopoverView.h"
#import "ModifyViewController.h"


@interface JournalListController ()<UITableViewDataSource,UITableViewDelegate>
{
    
    // 需要其他控件控制刷新
    MJRefreshNormalHeader* header;
    MJRefreshBackNormalFooter* footer;

    //CoverView *cover;
    NSString *key;
    UIView *WarningView;
    int b;
}

/**
 *   判断刷新 ，true -刷新  float -加载
 */
@property (assign, nonatomic) BOOL isToUpdate;
/**
 *  数据页数
 */
@property (assign, nonatomic)  int page;
/**
 *  日志列表存放数据
 */
@property (strong, nonatomic) NSMutableArray *publicmodelAry;
/**
 *  获取点击的日志id
 */
@property (assign, nonatomic) int JLC_log_id;
@property (strong, nonatomic)UIButton *notDataBtn;

@end

@implementation JournalListController


- (void)viewDidLoad {
    [super viewDidLoad];

    [self updateUI];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    self.title = [NSUD objectForKey:@"device_name"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



#pragma mark - TableView Data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.publicmodelAry.count;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cellHeight = [self tableView:tableView cellForRowAtIndexPath:indexPath];
    
    return CGRectGetHeight(cellHeight.frame);
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    JournalListCell * cell = [tableView dequeueReusableCellWithIdentifier:@"journalList"];
    
        cell.modelCellData =self.publicmodelAry[indexPath.row];
        
        cell.backgroundColor = RGB(33, 38, 44, 1);
        cell.selectionStyle = UITableViewCellSelectionStyleNone;//取消点击效果
        
    
  
    return cell;
}





#pragma mark - Other
// 更新数据，布局UI
- (void)updateUI {
    
    self.isToUpdate = YES;
    self.page = 0;
    self.journalTableView.tableFooterView=[[UIView alloc]init];//隐藏多余的分割线
    self.journalTableView.backgroundColor = RGB(33, 38, 44, 1);
    
    [self refreshTableView];
}



- (void)refreshTableView {
    
     header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.isToUpdate = YES;
        self.page = 0;
        [self requestUrlViewWithPage:self.page];
         
    }];
    header.automaticallyChangeAlpha = YES;
    header.lastUpdatedTimeLabel.hidden = YES;
    [header beginRefreshing];
    self.journalTableView.header = header;
    
     footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        self.isToUpdate = NO;
        b=self.page+19;
        b++;
        self.page = b;
        
        [self requestUrlViewWithPage:b];
    }];
    self.journalTableView.footer = footer;
    
}


- (void)requestUrlViewWithPage:(int)page {

    
   NSDictionary *parameterDic = @{ @"imie" : [NSUD objectForKey:@"id"],
                      @"iDisplayLength" : @"20",@"iDisplayStart":@(page),@"token":[NSUD objectForKey:@"token"] };
    
    [JournalViewModel requestWithUrlForJournal:self andParam:parameterDic success:^(NSMutableArray *model) {
        
        [header endRefreshing];
        [footer endRefreshing];

            if (_isToUpdate==YES) {
                    if (model == nil) {
                        [self.publicmodelAry removeAllObjects];
                        [self.notDataBtn removeFromSuperview];
                        [self.journalTableView addSubview:[self notDataBtn]];
                        [self.journalTableView reloadData];
                        [header endRefreshing];
                        [footer endRefreshing];


                    }else {
                        [self.publicmodelAry removeAllObjects];
                        for (NSDictionary *dic in model) {
                            [self.publicmodelAry addObject: dic];
                            
                        }
                        [self.notDataBtn removeFromSuperview];
                        [self.journalTableView reloadData];
                        [header endRefreshing];

                    }
                }else {
                    if (model==nil) {
                        [footer endRefreshing];
                    }else {
                        for (NSDictionary *dic in model) {
                            [self.publicmodelAry addObject: dic];
                        }
                        [self.journalTableView reloadData];
                        [footer endRefreshing];

                    }
                }
        
        
        
        
    } failure:^(NSString *error) {
              [self.notDataBtn removeFromSuperview];
        
                if (self.publicmodelAry.count ==0) {
                    [self.journalTableView addSubview:[self notDataBtn]];
                }
        
                [self.journalTableView.footer endRefreshing];
                [self.journalTableView.header endRefreshing];
                //新加
                [MBProgressHUD yty_showErrorWithTitle:nil detailsText:error toView:self.view];

    }];
    
    
}


-(NSMutableArray *)publicmodelAry
{
    if (!_publicmodelAry) {
        _publicmodelAry = [[NSMutableArray alloc]init];
    }
    return _publicmodelAry;
}


#pragma  mark - get / set
- (UIButton *)notDataBtn {
    if (!_notDataBtn) {
        _notDataBtn = [UtilToolsClss noDataBtn:NSLocalizedString(@"No Journal", @"没有日志")];
    }
    return _notDataBtn;
}


#pragma mark -Segue




//- (IBAction)delete:(UIButton *)sender {

//    PopoverAction *action1 = [PopoverAction actionWithImage:nil title:@"修改设备昵称" handler:^(PopoverAction *action) {
//
//        ModifyViewController *M = [[ModifyViewController alloc]init];
//        [self.navigationController pushViewController:M animated:YES];
//        
//    }];
    
//    PopoverAction *action2 = [PopoverAction actionWithImage:nil title:@"清空日志" handler:^(PopoverAction *action) {
//            if (self.publicmodelAry.count <=0) {
//                [MBProgressHUD yty_showErrorWithTitle:@"当前设备没有报警日志" detailsText:nil toView:nil];
//                
//            }else{
//                
//                GUAAlertView *v = [GUAAlertView alertViewWithTitle:@"提示"
//                                                           message:@" 确定清空当前设备报警日志？"
//                                                       buttonTitle:@"确定"
//                                               buttonTouchedAction:^{
//                                                   NSLog(@"button touched");
//        
//                                                   NSDictionary* dic = @{@"deviceNo":  [NSUD objectForKey:@"id"],@"deleteAlarms":@"all",@"token":[NSUD objectForKey:@"token"]};
//        
//                                                   NSLog(@"请求参数 ===== %@",dic);
//                                                   
//                                                   [JournalViewModel requestWithUrlForChangePassword:self andParam:dic success:^(JouList *model) {
//        
//                                                       if ([model isEqual:@(1)]) {
//                                                           [self updateUI];
//                                                       }
//                                                   } failure:^(NSString *error) {
//        
//                                                       [MBProgressHUD yty_showErrorWithTitle:nil detailsText:error toView:nil];
//        
//                                                   }];
//                
//                                               } dismissAction:^{
//                                                   NSLog(@"dismiss");
//                                                   
//                                               }];
//                [v show];
//        
//            }
//
//        
//        
//    }];

    
//    PopoverView *popoverView = [PopoverView popoverView];
//    popoverView.style = PopoverViewStyleDark;
//    [popoverView showToView:sender withActions:@[action1]];
//}







@end
